package com.htudata.live.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicInteger;

import com.htudata.live.dao.LiveDao;
import com.htudata.live.pojo.vo.DataTypesVO;
import com.htudata.live.pojo.vo.FlowVO;
import com.htudata.live.service.DataTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class DataTypeServiceImpl implements DataTypeService {

  @Autowired
  private LiveDao liveDao;
  
  public List<DataTypesVO> getVOList(String... dataTypeIds) {
    return getVOList2(1, dataTypeIds);
  }
  
  public List<DataTypesVO> getVOList2(int needAlarm, String... dataTypeIds) {
    List<Map<String, Object>> dataTypeAndFlowList = this.liveDao.selectDataTypeListAndFlowList();
    List<DataTypesVO> dataTypeVOList = new ArrayList<>();
    dataTypeAndFlowList.forEach(map -> {
          DataTypesVO dataTypesVO = null;
          if (dataTypeVOList.size() == 0) {
            dataTypesVO = new DataTypesVO();
            dataTypeVOList.add(dataTypesVO);
          } else {
            for (DataTypesVO dataTypesVO2 : dataTypeVOList) {
              if (String.valueOf(dataTypesVO2.getDataTypeId()).equals(map.get("data_type_id").toString()))
                dataTypesVO = dataTypesVO2; 
            } 
          } 
          if (Objects.isNull(dataTypesVO)) {
            dataTypesVO = new DataTypesVO();
            dataTypeVOList.add(dataTypesVO);
          } 
          dataTypesVO = HandleMapToDataTypesVO(map, dataTypesVO);
        });
    if (needAlarm == 1)
      getDataTypeAndFlowAlarms(dataTypeVOList);
    return dataTypeVOList;
  }
  
  public DataTypesVO HandleMapToDataTypesVO(Map<String, Object> map, DataTypesVO dataTypesVO) {
    dataTypesVO.setDataTypeId(Integer.valueOf(map.get("data_type_id").toString()).intValue());
    dataTypesVO.setName(map.get("type_zh").toString());
    if (map.containsKey("flow_id")) {
      FlowVO flowsVO = new FlowVO();
      flowsVO.setFlowId(map.get("flow_id").toString());
      flowsVO.setDataTypeId(Integer.valueOf(map.get("data_type_id").toString()).intValue());
      flowsVO.setName(map.get("name").toString());
      dataTypesVO.getChild().add(flowsVO);
    } 
    return dataTypesVO;
  }
  
  public void getDemoDataTypeAndFlowAlarms(List<DataTypesVO> dataTypeVOList) {
    for (DataTypesVO dataTypeVO : dataTypeVOList) {
      dataTypeVO.setAlarmNum(10);
      dataTypeVO.setItemNum(200);
      dataTypeVO.getChild().forEach(flowVO -> {
            flowVO.setAlarmNum(5);
            flowVO.setItemNum(100);
          });
    } 
  }
  
  public void getDataTypeAndFlowAlarms(List<DataTypesVO> dataTypeVOList) {
    JSONArray nodeLocalProblems = this.liveDao.countNodeProblem();
    JSONArray flowLocalItems = this.liveDao.countFlowItem();
    for (Iterator<DataTypesVO> iterator = dataTypeVOList.iterator(); iterator.hasNext(); ) {
      DataTypesVO dataTypeVO = iterator.next();
      dataTypeVO.getChild().forEach(flowVO -> {
            countFlowItemAndProblem(flowVO, nodeLocalProblems, flowLocalItems);
            dataTypeVO.setAlarmNum(dataTypeVO.getAlarmNum() + flowVO.getAlarmNum());
            dataTypeVO.setItemNum(dataTypeVO.getItemNum() + flowVO.getItemNum());
          });
    } 
  }
  
  public void countFlowItemAndProblem(FlowVO flowVO, JSONArray nodeProblems, JSONArray flowItems) {
    AtomicInteger alarmNum = new AtomicInteger();
    AtomicInteger itemNum = new AtomicInteger();
    String flowId = flowVO.getFlowId();
    flowItems.forEach(flowItem -> {
          JSONObject flowZabbixItemJson = (JSONObject)flowItem;
          if (flowId.equals(flowZabbixItemJson.getString("flow_id")))
            itemNum.set(flowZabbixItemJson.getIntValue("item_count")); 
        });
    nodeProblems.forEach(nodeProblem -> {
          JSONObject nodeProblemJson = (JSONObject)nodeProblem;
          if (flowId.equals(nodeProblemJson.getString("flow_id"))) {
            System.out.println(nodeProblemJson.getString("node_id") + "=" + nodeProblemJson.getIntValue("problem_count"));
            alarmNum.set(alarmNum.get() + nodeProblemJson.getIntValue("problem_count"));
          } 
        });
    flowVO.setAlarmNum(flowVO.getAlarmNum() + alarmNum.get());
    flowVO.setItemNum(flowVO.getItemNum() + itemNum.get());
  }
  
  public JSONObject selectGlobalConf() {
    return this.liveDao.selectGlobalConf();
  }
}
